﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CharitiesLib;
using System.Security.Cryptography;

namespace Charities
{
    public partial class AddNewUser : Form
    {
        public bool is_loaded = false;
        Form fo = null;
        Form frfo = null;
        public AddNewUser(Form fromForm ,Form m)
        {
            this.MdiParent = m;
            frfo = fromForm;
            InitializeComponent();
            AcceptButton = btn_Add;
        }
        int uid = 0;
        public AddNewUser(int i,Form fromForm,Form m)
        {
            fo = m;
            this.MdiParent = m;
            uid = i;
            InitializeComponent();
            AcceptButton = btn_update;
            frfo = fromForm;
        }
       
        public delegate void UpdateFormHandler(object sender, UpdateFormEventArg e);
        public event UpdateFormHandler FormUpdated;

        private void btn_Dispose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        BindingSource dbc = new BindingSource();
        ModelContainer ctx = new ModelContainer();
        private void AddNewUser_Load(object sender, EventArgs e)
        {
            is_loaded = true;
            // add this form to window list
            //((Forms.FormMDI)fo).addFormToWindowsList(this);
            dbc.DataSource = (from r in ctx.Roles select r).ToList();
            cbo_Role.DataSource = dbc;
            if (uid > 0)
            {
                User u = ctx.Users.Single(c => c.Id == uid);
                txbUsername.Text = u.username;
                cbo_Role.SelectedValue = u.RoleId;
                btn_update.Enabled = true;
                btn_Add.Enabled = false;
                txbUsername.Enabled = false;
            }
            
        }
        public static string Hash(string value)
        {
            var sha = new System.Security.Cryptography.SHA1CryptoServiceProvider();
            byte[] hash = sha.ComputeHash(Encoding.ASCII.GetBytes(value));
            return BytesToHex(hash).ToLower();
        }

        private static string BytesToHex(byte[] bytes)
        {
            return String.Concat(Array.ConvertAll(bytes, x => x.ToString("X2")));
        }
        private void btn_Add_Click(object sender, EventArgs e)
        {
            try
            {
                User u = new User();
                u.username = txbUsername.Text.Trim();

                u.password = Hash(txbPassword.Text.Trim());
                Role r = ctx.Roles.Single(c => c.Name == cbo_Role.Text);
                u.RoleId = r.Id;
                u.InsertDate = DateTime.Now;
                u.InsertUId = 1;
                ctx.Users.Add(u);
                txbPassword.Text = "";
                txbUsername.Text = "";
                ctx.SaveChanges();
                UpdateFormEventArg arg = new UpdateFormEventArg(true);
                FormUpdated(this, arg);
            }
            catch (Exception)
            {

                MessageBox.Show("مشكلة في الاتصال بقاعدة البيانات");
            }
            
        }

        private void btn_update_Click(object sender, EventArgs e)
        {
            try
            {
                User u = ctx.Users.Single(c => c.Id == uid);
                u.username = txbUsername.Text.Trim();
                u.password = Hash(txbPassword.Text.Trim());
                Role r = ctx.Roles.Single(c => c.Name == cbo_Role.Text);
                u.RoleId = r.Id;
                u.UpdateDate = DateTime.Now;
                u.UpdateUId = 1;
                ctx.SaveChanges();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message); ;
            }
            //UpdateFormEventArg arg = new UpdateFormEventArg(true);
            //FormUpdated(this, arg);
            this.Dispose();
        }

        private void AddNewUser_FormClosed(object sender, FormClosedEventArgs e)
        {
            is_loaded = false;
            // remove this form from windows list
            //((Forms.FormMDI)fo).removeFormFromWindowsList(this);
            ((Forms.FormMDI)this.MdiParent).activateWindow(frfo);
        }

        private void AddNewUser_Activated(object sender, EventArgs e)
        {
            // activate this form and add it windows list as activate one
            ((Forms.FormMDI)this.MdiParent).activateWindow(this);
        }

    }
}